package com.tj.bo.account.server.services;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.HttpUtil;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.tj.bo.account.client.AccountBalanceChangeService;
import com.tj.bo.account.client.AccountQueryService;
import com.tj.bo.account.client.WithdrawService;
import com.tj.bo.account.client.enums.AccountStateEnum;
import com.tj.bo.account.client.enums.ExpenditureEnum;
import com.tj.bo.account.client.enums.WithDrawStatusEnum;
import com.tj.bo.account.client.vo.*;
import com.tj.bo.account.server.dao.AccountExternalDao;
import com.tj.bo.account.server.dao.OrderDepositDao;
import com.tj.bo.account.server.dao.OrderWithdrawDao;
import com.tj.bo.account.server.entity.AccountExternalEntity;
import com.tj.bo.account.server.entity.OrderWithdrawEntity;
import com.tj.bo.common.conf.config.RedisConfigUtil;
import com.tj.bo.common.conf.util.PrivacyDimmerUtil;
import com.tj.bo.common.conf.util.SerialNumberUtil;
import com.tj.bo.integration.PaymentIntegrationService;
import com.tj.bo.integration.input.PayoutInput;
import com.tj.bo.integration.output.PayoutOutput;
import com.tj.bo.payment.client.PayCommandService;
import com.tj.bo.payment.client.vo.DfPayApplicationVO;
import com.tj.bo.payment.client.vo.PaymentOrderVO;
import com.tj.bo.user.client.UserRegisterService;
import com.tj.bo.user.client.vo.input.UserAuthenticationRO;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.*;

/**
 * 提现服务
 * User: anonymous
 * Date: 2020/06/11
 * Time: 下午2:16
 */
@Service
public class WithdrawServiceImpl implements WithdrawService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WithdrawServiceImpl.class);

    public static final String PAYOUT_SUCCESS = "0000";
    public static final String PAYOUT_FAILED = "0001";
    @Autowired
    private AccountExternalDao accountExternalDao;
    @Autowired
    private OrderWithdrawDao orderWithdrawDao;
    @Autowired
    private OrderDepositDao orderDepositDao;
    @Autowired
    private AccountBalanceChangeService accountBalanceChangeService;
    @Autowired
    private AccountQueryService accountQueryService;
    @Autowired
    private PaymentIntegrationService paymentIntegrationService;
    @Autowired
    private RedisConfigUtil accountConfig;
    @Autowired
    private UserRegisterService userRegisterService;
    @Autowired
    private PayCommandService payCommandService;

//    @PostConstruct
//    public void init(){
//        WithDrawAuditCallbackVO withDrawAuditCallbackVO = new WithDrawAuditCallbackVO();
//        withDrawAuditCallbackVO.setWithdrawOrderNo("20210721140838d51267WD000001");
//        withDrawAuditCallbackVO.setUserCode("20210720150723d51685UC000002");
//        withDrawAuditCallbackVO.setPass(true);
//        withDrawAuditCallbackVO.setAuditor("SYSTEM");
//        withDrawAuditCallbackVO.setReasons("");
//        withDrawAuditNotify(withDrawAuditCallbackVO);
//    }

    @Override
    public void updateWithdrawStatusByWithdrawNo(String withdrawNo, int status) {
        if (StringUtils.isEmpty(withdrawNo)) {
            return;
        }

        OrderWithdrawEntity orderWithdrawEntity = orderWithdrawDao.selectByWithdrawOrderNo(withdrawNo);
        if (orderWithdrawEntity == null) {
            return;
        }

        if (orderWithdrawEntity.getStatus() == status) {
            return;
        }

        //todo 需要状态机
        orderWithdrawEntity.setStatus(status);
        orderWithdrawDao.updateByPrimaryKey(orderWithdrawEntity);
    }


    /**
     * 获取银行名称
     */
    @Override
    public String getBankName(String ifscCode) {
        try {
            String data = HttpUtil.getPage("https://ifsc.razorpay.com/" + ifscCode);
            JSONObject jsonObject = JSONObject.parseObject(data);
            return jsonObject.getString("BANK");
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 绑定提现账号
     *
     * @param withDrawAccountVO
     */
    @Override
    public void bindWithDrawAccount(WithDrawAccountVO withDrawAccountVO) {
        AccountExternalEntity accountExternalEntity = accountExternalDao.selectExternalAccount(withDrawAccountVO.getUserCode());
        if (accountExternalEntity != null) {
            if (accountExternalEntity.getBankAccountNo().equals(withDrawAccountVO.getBankAccountNo())) {
                LOGGER.info("卡号相同不处理,withDrawAccountVO:{}", JSON.toJSONString(withDrawAccountVO));
                return;
            } else {
                int num = accountExternalDao.deleteAccountExternalByUserCode(withDrawAccountVO.getUserCode());
                LOGGER.info("删除旧卡成功,num:{},withDrawAccountVO:{}", num, JSON.toJSONString(withDrawAccountVO));
            }
        }
        AccountExternalEntity newAccountExternalEntity = new AccountExternalEntity();
        BeanUtils.copyProperties(withDrawAccountVO, newAccountExternalEntity);
        accountExternalDao.insert(newAccountExternalEntity);

        UserAuthenticationRO userAuthenticationRO = new UserAuthenticationRO();
        userAuthenticationRO.setUserCode(withDrawAccountVO.getUserCode());
        userAuthenticationRO.setUserName(withDrawAccountVO.getUserName());
        userAuthenticationRO.setMobile(withDrawAccountVO.getUserMobile());
        userAuthenticationRO.setEmail(withDrawAccountVO.getUserEmail());
        userRegisterService.userAuthentication(userAuthenticationRO);
    }

    /**
     * 查询提现账号
     */
    @Override
    public WithDrawAccountVO queryWithDrawAccount(String userCode) {
        AccountExternalEntity accountExternalEntity = accountExternalDao.selectExternalAccount(userCode);
        if (accountExternalEntity == null) {
            return new WithDrawAccountVO();
        }
        WithDrawAccountVO withDrawAccountVO = new WithDrawAccountVO();
        BeanUtils.copyProperties(accountExternalEntity, withDrawAccountVO);
        return withDrawAccountVO;
    }

    @Override
    public WithdrawRecordVO getWithdrawRecordByWithdrawNo(String withdrawNo) {
        if (StringUtils.isEmpty(withdrawNo)) {
            return null;
        }

        OrderWithdrawEntity orderWithdrawEntity = orderWithdrawDao.selectByWithdrawOrderNo(withdrawNo);
        if (orderWithdrawEntity == null) {
            return null;
        }


        return new WithdrawRecordVO()
                .setAccount(orderWithdrawEntity.getBankAccountNo())
                .setAmount(orderWithdrawEntity.getAmount())
                .setApplyTime(DateUtil.getDate(orderWithdrawEntity.getGmtCreate(), "yyyy-MM-dd HH:mm:ss"))
                .setStatus(WithDrawStatusEnum.getByCode(orderWithdrawEntity.getStatus()).getDesc())
                .setFailureReasons(orderWithdrawEntity.getFailureReasons())
                .setStatusCode(orderWithdrawEntity.getStatus());
    }

    /**
     * 提现
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void withDraw(WithDrawVO withDrawVO) {
        String withdrawOrderNo = SerialNumberUtil.getBizOrderNumber("W", "D");
        withDrawVO.setWithdrawOrderNo(withdrawOrderNo);
        //check 提现金额
        checkWithDrawAmount(withDrawVO);
        //query 提现账号
        queryWithDrawAccount(withDrawVO);
        //check 最新一笔订单状态
        checkLatestOrder(withDrawVO);
        //冻结金额
        AccountFreezeVO accountFreezeVO = new AccountFreezeVO();
        accountFreezeVO.setUserCode(withDrawVO.getUserCode());
        accountFreezeVO.setOrderNo(withdrawOrderNo);
        accountFreezeVO.setAmount(withDrawVO.getAmount());
        boolean freeze = accountBalanceChangeService.freeze(accountFreezeVO);
        if (!freeze) {
            throw new ApplicationException("Insufficient balance");
        }
        //创建提现订单
        createWithDrawOrder(withDrawVO);
    }

    /**
     * 自动审核
     *
     * @param withDrawVO
     */
    @Override
    @Async
    public String withDrawAutoAuditCheck(WithDrawVO withDrawVO) {
        if (StringUtils.isEmpty(withDrawVO.getWithdrawOrderNo())) {
            return "订单号为空";
        }
        LOGGER.info("withDrawAutoAuditCheck,触发提现自动审核,withdrawOrderNo:{}", withDrawVO.getWithdrawOrderNo());
        OrderWithdrawEntity orderWithdrawEntity = orderWithdrawDao.selectByWithdrawOrderNo(withDrawVO.getWithdrawOrderNo());
        if (orderWithdrawEntity == null || StringUtils.isEmpty(orderWithdrawEntity.getUserCode())) {
            return "订单不存在";
        }

        BigDecimal totalDepositAmount = orderDepositDao.selectTotalDepositAmount(orderWithdrawEntity.getUserCode());
        BigDecimal totalWithdrawAmount = orderWithdrawDao.selectTotalWithdrawAmount(orderWithdrawEntity.getUserCode());

        String reasons = "";
        boolean pass = false;
        if (totalDepositAmount != null) {
            if (totalWithdrawAmount == null || totalWithdrawAmount.compareTo(orderWithdrawEntity.getAmount()) == 0) {
                pass = true;
                reasons = "首次提现-自动通过";
            } else if (totalDepositAmount.doubleValue() * 2 > totalWithdrawAmount.doubleValue()) {
                pass = true;
                reasons = "累计提现小于两倍累计充值-自动通过";
            }
        }
        LOGGER.info("withDrawAutoAuditCheck,处理结果,totalDepositAmount:{},totalWithdrawAmount:{},pass:{},reasons:{}", totalDepositAmount, totalWithdrawAmount, pass, reasons);

        if (pass) {
            WithDrawAuditCallbackVO withDrawAuditCallbackVO = new WithDrawAuditCallbackVO();
            withDrawAuditCallbackVO.setWithdrawOrderNo(withDrawVO.getWithdrawOrderNo());
            withDrawAuditCallbackVO.setUserCode(orderWithdrawEntity.getUserCode());
            withDrawAuditCallbackVO.setPass(pass);
            withDrawAuditCallbackVO.setAuditor("SYSTEM");
            withDrawAuditCallbackVO.setReasons(reasons);

            this.withDrawAuditNotify(withDrawAuditCallbackVO);
            return "自动通过";
        } else {
            return "暂不处理";
        }
    }

    /**
     * 审核回调
     */
    @Override
    public void withDrawAuditNotify(WithDrawAuditCallbackVO withDrawAuditCallbackVO) {
        OrderWithdrawEntity orderWithdrawEntity = orderWithdrawDao.selectByWithdrawOrderNo(withDrawAuditCallbackVO.getWithdrawOrderNo());
        Integer status = orderWithdrawEntity.getStatus();
        if (status.equals(WithDrawStatusEnum.AUDIT_PROCESSING.getCode()) || (status.equals(WithDrawStatusEnum.PAYOUT_PROCESSING.getCode()) && orderWithdrawEntity.getDelay() == 1)) {


            if (withDrawAuditCallbackVO.getPass()) {
                orderWithdrawEntity.setStatus(WithDrawStatusEnum.PAYOUT_PROCESSING.getCode());
                //推送到支付中心
//                PayoutInput payoutInput = generatePayoutInput(orderWithdrawEntity);
//                PayoutOutput payout = paymentIntegrationService.payout(accountConfig.getPayBaseUrl(), payoutInput);
                DfPayApplicationVO dfPayApplicationVO = new DfPayApplicationVO();
                dfPayApplicationVO.setBankName(orderWithdrawEntity.getBankName());
                dfPayApplicationVO.setAccountName(orderWithdrawEntity.getAccountNo());
                dfPayApplicationVO.setCardNumber(orderWithdrawEntity.getBankAccountNo());
                dfPayApplicationVO.setMoney(orderWithdrawEntity.getAmount().intValue());
                dfPayApplicationVO.setSubbranch(orderWithdrawEntity.getBankName());
                dfPayApplicationVO.setProvince(orderWithdrawEntity.getIfscCode());
                dfPayApplicationVO.setCity(orderWithdrawEntity.getIfscCode());
                PaymentOrderVO createDfPayOrder = payCommandService.createDfPayOrder(dfPayApplicationVO);
                String acceptNo = createDfPayOrder.getOrderCode();
                orderWithdrawEntity.setPayAcceptNo(acceptNo);
                orderWithdrawEntity.setFailureReasons(withDrawAuditCallbackVO.getReasons());
                orderWithdrawEntity.setAuditor(withDrawAuditCallbackVO.getAuditor());
            } else {
                //解冻余额
                AccountUnFreezeVO accountUnFreezeVO = new AccountUnFreezeVO();
                accountUnFreezeVO.setUserCode(orderWithdrawEntity.getUserCode());
                accountUnFreezeVO.setOrderNo(orderWithdrawEntity.getWithdrawOrderNo());
                accountUnFreezeVO.setAmount(orderWithdrawEntity.getAmount());
                boolean result = accountBalanceChangeService.unfreeze(accountUnFreezeVO);
                if (!result) {
                    LOGGER.error("提现失败解冻失败,withDrawAuditCallbackVO:{}", JSON.toJSONString(withDrawAuditCallbackVO));
                }

                orderWithdrawEntity.setStatus(WithDrawStatusEnum.AUDIT_REJECT.getCode());
                orderWithdrawEntity.setFailureReasons(withDrawAuditCallbackVO.getReasons());
                orderWithdrawEntity.setAuditor(withDrawAuditCallbackVO.getAuditor());
            }

            orderWithdrawEntity.setDelay(0);
            orderWithdrawDao.updateByPrimaryKey(orderWithdrawEntity);
        } else {
            throw new ApplicationException("withdrawstatus error");
        }
    }

    /**
     * 打款通知
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void withDrawPayoutNotify(WithDrawPayoutNotifyVO withDrawPayoutNotifyVO) {
        OrderWithdrawEntity orderWithdrawEntity = orderWithdrawDao.selectByWithdrawOrderNo(withDrawPayoutNotifyVO.getWithdrawOrderNo());
        orderWithdrawEntity.setWithdrawChannel(withDrawPayoutNotifyVO.getPaymentChannelName());
        orderWithdrawEntity.setPayAcceptNo(withDrawPayoutNotifyVO.getPayAcceptNo());
        if (!orderWithdrawEntity.getStatus().equals(WithDrawStatusEnum.PAYOUT_PROCESSING.getCode())) {
            throw new ApplicationException("withdrawstatus error");
        }

        if (PAYOUT_SUCCESS.equals(withDrawPayoutNotifyVO.getCode())) {
            //更改状态
            orderWithdrawEntity.setStatus(WithDrawStatusEnum.PAYOUT_SUCCESS.getCode());
            orderWithdrawDao.updateByPrimaryKey(orderWithdrawEntity);
            //扣减账号冻结金额
            AccountExpenditureVO accountExpenditureVO = new AccountExpenditureVO();
            accountExpenditureVO.setUserCode(orderWithdrawEntity.getUserCode());
            accountExpenditureVO.setOrderNo(orderWithdrawEntity.getWithdrawOrderNo());
            accountExpenditureVO.setAmount(orderWithdrawEntity.getAmount());
            accountExpenditureVO.setExpenditureEnum(ExpenditureEnum.WITHDRAW);
            accountBalanceChangeService.expenditure(accountExpenditureVO);
        } else {
            //更改状态
            orderWithdrawEntity.setStatus(WithDrawStatusEnum.PAYOUT_FAILED.getCode());
            orderWithdrawEntity.setFailureReasons(withDrawPayoutNotifyVO.getMessage());
            orderWithdrawDao.updateByPrimaryKey(orderWithdrawEntity);
            //解冻余额
            AccountUnFreezeVO accountUnFreezeVO = new AccountUnFreezeVO();
            accountUnFreezeVO.setUserCode(orderWithdrawEntity.getUserCode());
            accountUnFreezeVO.setOrderNo(orderWithdrawEntity.getWithdrawOrderNo());
            accountUnFreezeVO.setAmount(orderWithdrawEntity.getAmount());
            accountBalanceChangeService.unfreeze(accountUnFreezeVO);
        }
    }

    /**
     * 获取提现记录
     */
    @Override
    public List<WithdrawRecordVO> getWithdrawRecordList(String userCode) {
        Map<String, Object> params = new HashMap<>();
        Page page = new Page(500);

        params.put("userCode", userCode);
        params.put("deleted", 0);
        params.put("page", page);
        params.put("orderBy", " id desc ");

        List<OrderWithdrawEntity> orderWithdrawEntities = orderWithdrawDao.selectByParams(params);
        List<WithdrawRecordVO> withdrawRecordVOList = new ArrayList<>();
        for (OrderWithdrawEntity orderWithdrawEntity : orderWithdrawEntities) {
            WithdrawRecordVO withdrawRecordVO = new WithdrawRecordVO();
            withdrawRecordVO.setApplyTime(DateUtil.getDate(orderWithdrawEntity.getGmtCreate(), "yyyy-MM-dd HH:mm:ss"));
            withdrawRecordVO.setAmount(orderWithdrawEntity.getAmount());
            withdrawRecordVO.setStatus(WithDrawStatusEnum.getByCode(orderWithdrawEntity.getStatus()).getDesc());
            withdrawRecordVO.setAccount(PrivacyDimmerUtil.maskBankCard(orderWithdrawEntity.getBankAccountNo()));
            withdrawRecordVOList.add(withdrawRecordVO);
        }
        ;
        return withdrawRecordVOList;
    }

    /**
     * 获取提现进度
     */
    @Override
    public WithdrawRecordVO queryWithDrawProgress(String userCode) {
        OrderWithdrawEntity orderWithdrawEntity = orderWithdrawDao.selectLatestOrder(userCode);
        WithdrawRecordVO withdrawRecordVO = null;
        if (orderWithdrawEntity != null) {
            withdrawRecordVO = new WithdrawRecordVO();
            withdrawRecordVO.setApplyTime(DateUtil.getDate(orderWithdrawEntity.getGmtCreate(), "yyyy-MM-dd"));
            withdrawRecordVO.setAmount(orderWithdrawEntity.getAmount());
            withdrawRecordVO.setStatusCode(orderWithdrawEntity.getStatus());
            withdrawRecordVO.setStatus(WithDrawStatusEnum.getByCode(orderWithdrawEntity.getStatus()).getDesc());
            withdrawRecordVO.setAccount(PrivacyDimmerUtil.maskBankCard(orderWithdrawEntity.getBankAccountNo()));
            //withdrawRecordVO.setAccount(StringUtils.isNotEmpty(orderWithdrawEntity.getUserEmail()) ? orderWithdrawEntity.getUserEmail() : orderWithdrawEntity.getUserName());
            withdrawRecordVO.setFailureReasons(orderWithdrawEntity.getFailureReasons());
        }
        return withdrawRecordVO;
    }

    /**
     * 获取提现账号
     */
    private void queryWithDrawAccount(WithDrawVO withDrawVO) {
        AccountVO accountInfo = accountQueryService.getAccountInfo(withDrawVO.getUserCode());
        if (accountInfo.getStatus() == AccountStateEnum.INVALID.getCode()) {
            throw new ApplicationException(" Account status abnormal");
        }
        if (!accountInfo.isAllowWithdrawal()) {
            throw new ApplicationException(" Account status abnormal");
        }
        AccountExternalEntity accountExternalEntity = accountExternalDao.selectExternalAccount(withDrawVO.getUserCode());
        if (accountExternalEntity == null) {
            throw new ApplicationException("Please bind withdrawal card");
        }
        //如果之前没有存储过aadhaar 则进行存储
        if (StringUtils.isEmpty(accountExternalEntity.getAadhaarFront())) {
            accountExternalEntity.setAadhaarFront(withDrawVO.getAadhaarFront());
        }
        accountExternalEntity.setUserMobile(withDrawVO.getUserMobile());
        accountExternalDao.updateByPrimaryKey(accountExternalEntity);
        withDrawVO.setBankAccountNo(accountExternalEntity.getBankAccountNo());
        withDrawVO.setIfscCode(accountExternalEntity.getIfscCode());
        withDrawVO.setUserName(accountExternalEntity.getUserName());
        withDrawVO.setBankName(accountExternalEntity.getBankName());
        withDrawVO.setAccountNo(accountInfo.getAccountNo());
    }

    /**
     * 创建提现订单
     */
    private void createWithDrawOrder(WithDrawVO withDrawVO) {
        OrderWithdrawEntity orderWithdrawEntity = new OrderWithdrawEntity();
        BeanUtils.copyProperties(withDrawVO, orderWithdrawEntity);
        orderWithdrawEntity.setStatus(WithDrawStatusEnum.AUDIT_PROCESSING.getCode());
        orderWithdrawDao.insert(orderWithdrawEntity);
    }

    /**
     * check 提现金额
     */
    private void checkWithDrawAmount(WithDrawVO withDrawVO) {
        if (withDrawVO.getAmount().compareTo(BigDecimal.valueOf(accountConfig.getMinWithdrawalAmount())) < 0) {
            throw new ApplicationException("Withdrawal amount needs to be greater than PKR" + accountConfig.getMinWithdrawalAmount());
        }
        if (withDrawVO.getAmount().compareTo(BigDecimal.valueOf(accountConfig.getMaxWithdrawalAmount())) > 0) {
            throw new ApplicationException("The withdrawal amount needs to be less than PKR" + accountConfig.getMaxWithdrawalAmount());
        }
        AccountVO accountInfo = accountQueryService.getAccountInfo(withDrawVO.getUserCode());
        if (withDrawVO.getAmount().compareTo(accountInfo.getCanWithdrawalAmount()) > 0) {
            throw new ApplicationException("Insufficient balance");
        }
    }

    private PayoutInput generatePayoutInput(OrderWithdrawEntity orderWithdrawEntity) {
        PayoutInput payoutInput = new PayoutInput();
        payoutInput.setProductCategoryCode(accountConfig.getProductCategory());
        payoutInput.setUserCode(orderWithdrawEntity.getUserCode());
        payoutInput.setIfsc(orderWithdrawEntity.getIfscCode());
        payoutInput.setEmail(orderWithdrawEntity.getUserEmail());
        payoutInput.setName(orderWithdrawEntity.getUserName());
        payoutInput.setMobile(orderWithdrawEntity.getUserMobile());
        payoutInput.setArrivalAmount(orderWithdrawEntity.getAmount().toString());
        payoutInput.setContractAmount(orderWithdrawEntity.getAmount().toString());
        payoutInput.setBizNo(orderWithdrawEntity.getWithdrawOrderNo());
        payoutInput.setCardNo(orderWithdrawEntity.getBankAccountNo());
        payoutInput.setBankName(orderWithdrawEntity.getBankName());
        payoutInput.setNotifyUrl(accountConfig.getDomainUrl() + "withdraw/payout/notify");

        return payoutInput;
    }

    /**
     * 检查最后一笔订单状态
     */
    private void checkLatestOrder(WithDrawVO withDrawVO) {
        WithdrawRecordVO withdrawRecordVO = this.queryWithDrawProgress(withDrawVO.getUserCode());
        List<Integer> codes = Arrays.asList(WithDrawStatusEnum.INIT.getCode(), WithDrawStatusEnum.AUDIT_PROCESSING.getCode(), WithDrawStatusEnum.PAYOUT_PROCESSING.getCode());
        boolean withdrawProcessing = withdrawRecordVO != null && codes.contains(withdrawRecordVO.getStatusCode());
        if (withdrawProcessing) {
            throw new ApplicationException("The last withdrawal request is being processed");
        }

        boolean depositMoreOrderInOneDay = true;
        List<OrderWithdrawEntity> orderWithdrawEntities = orderWithdrawDao.selectLatestOrders(withDrawVO.getUserCode());
        if (CollectionUtils.isNotEmpty(orderWithdrawEntities)) {
            for (OrderWithdrawEntity entity : orderWithdrawEntities) {
                if (entity.getStatus() == WithDrawStatusEnum.PAYOUT_SUCCESS.getCode() && DateUtils.isSameDay(new Date(), entity.getGmtCreate())) {
                    depositMoreOrderInOneDay = false;
                    break;
                }
            }
        }
        if (!depositMoreOrderInOneDay) {
            throw new ApplicationException("You have made a successful withdrawal today. Please come back tomorrow.");
        }
    }

    @Override
    public BigDecimal selectTotalDepositAmount(String userCode) {
        return orderDepositDao.selectTotalDepositAmount(userCode);
    }
}